package com.bonc.rms.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日历工具类，要求时间字符串格式为：yyyy-MM-dd
 * 
 * @author XHRGL
 *
 */
public class CalendarUtil {

	/**
	 * 将字符串转为 Calendar 对象
	 * 
	 * @param dateStr 日期字符串
	 * @param separator dateStr 年月日之间的分隔符
	 * @return
	 */
	public static Calendar convertToCal(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar retCal = Calendar.getInstance();
		try {
			Date date = sdf.parse(dateStr);
			retCal.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return retCal;
	}
	
	/**
	 * 计算两个日期的年差值
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int calYearDif(String startDate, String endDate) {
		return calYearDif(convertToCal(startDate), convertToCal(endDate));
	}
	
	/**
	 * 计算两个日期的年差值
	 * 
	 * @param startDateCal
	 * @param endDateCal
	 * @return
	 */
	public static int calYearDif(Calendar startDateCal, Calendar endDateCal) {
		
		int startYear = startDateCal.get(Calendar.YEAR);
		int endYear = endDateCal.get(Calendar.YEAR);
		
		return endYear - startYear;
	}
	
	/**
	 * 计算两个日期的月差值
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int calMonthDif(String startDate, String endDate) {
		
		Calendar startDateCal = convertToCal(startDate);
		Calendar endDateCal = convertToCal(endDate);
		
		return calMonthDif(startDateCal, endDateCal);
	}
	
	/**
	 * 计算两个日期的月差值
	 * 
	 * @param startDateCal
	 * @param endDateCal
	 * @return
	 */
	public static int calMonthDif(Calendar startDateCal, Calendar endDateCal) {
		
		int startYear = startDateCal.get(Calendar.YEAR);
		int endYear = endDateCal.get(Calendar.YEAR);
		int startMonth = startDateCal.get(Calendar.MONTH);
		int endMonth = endDateCal.get(Calendar.MONTH);
		
		return (endYear - startYear) * 12 + (endMonth - startMonth);
	}
	
	/**
	 * 计算两个日期的日差值
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int calDayDif(String startDate, String endDate) {
		return calDayDif(convertToCal(startDate), convertToCal(endDate));
	}
	
	/**
	 * 计算两个日期的日差值
	 * 
	 * @param startDateCal
	 * @param endDateCal
	 * @return
	 */
	public static int calDayDif(Calendar startDateCal, Calendar endDateCal) {
		
		long startTime = startDateCal.getTimeInMillis();
		long endTime = endDateCal.getTimeInMillis();
		long dif = endTime - startTime;
		
		return (int) (dif / (24 * 60 * 60 * 1000));
	}
	
	/**
	 * 获取两日期之间的月份列表
	 * 
	 * @param startMonth
	 * @param endMonth
	 * @return
	 */
	public static List<String> getMonthList(String startDate, String endDate) {
		return getMonthList(convertToCal(startDate), convertToCal(endDate));
	}
	
	/**
	 * 获取两日期之间的月份列表
	 * 
	 * @param startMonth
	 * @param endMonth
	 * @return
	 */
	public static List<String> getMonthList(Calendar startDateCal, Calendar endDateCal) {
		startDateCal.set(Calendar.DAY_OF_MONTH, 1);
		endDateCal.set(Calendar.DAY_OF_MONTH, 1);
		
		List<String> monthList = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		while(!endDateCal.equals(startDateCal)) {
			monthList.add(sdf.format(startDateCal.getTime()));
			startDateCal.add(Calendar.MONTH, 1);
		}
		monthList.add(sdf.format(startDateCal.getTime()));
		
		return monthList;
	}
	
}
